package com.hyf.meterialdesign.activity;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.hyf.meterialdesign.R;
import com.hyf.meterialdesign.fragment.SimpleFragment;

import java.util.ArrayList;
import java.util.List;

public class FunscreenFragmentActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private Toolbar toolbar;

    private List<SimpleFragment> fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_funscreen_fragment);

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        initData();
    }

    private void initData() {
        toolbar.setTitle("这是标题");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        fragmentList = new ArrayList<>();
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491475161894&di=92e9caa3884ccdcc7f41b86854011c10&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fa5c27d1ed21b0ef47a3cc0a7dbc451da80cb3e76.jpg",true));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491475161892&di=95fc892c3e2a58be54df5df563ee4fba&imgtype=0&src=http%3A%2F%2Fb.zol-img.com.cn%2Fdesk%2Fbizhi%2Fimage%2F4%2F960x600%2F1383618780138.jpg",true));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491475161891&di=88fd11ea838c1df15a2dbf878b103743&imgtype=0&src=http%3A%2F%2Fb.zol-img.com.cn%2Fdesk%2Fbizhi%2Fimage%2F3%2F960x600%2F137661748897.jpg",true));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491475220377&di=0ab4d550febb3a7870631e2e8f392429&imgtype=jpg&src=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D549815775%2C1096048392%26fm%3D214%26gp%3D0.jpg",true));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491475161888&di=5a0c6fface1c459504e6b6abfe5683e5&imgtype=0&src=http%3A%2F%2Fb.zol-img.com.cn%2Fdesk%2Fbizhi%2Fimage%2F4%2F960x600%2F1387880549451.jpg",true));
        viewPager.setAdapter(new SimpleFragmentAdapter(getSupportFragmentManager(),fragmentList));
    }

    public void showToast(View view) {
        Toast.makeText(this, "toast", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    class SimpleFragmentAdapter extends FragmentPagerAdapter{

        private List<SimpleFragment> fragmentList;

        public SimpleFragmentAdapter(FragmentManager fm,List<SimpleFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
